package blazingbubble.network.game.strategy;

import blazingbubble.game.Game;
import blazingbubble.game.Result;
import moltencore.network.protocol.GameEndedNotification;
import org.apache.log4j.Logger;
import spark.network.protocol.Response;
import spark.network.strategy.IProtocolHandleStrategy;
import spark.network.protocol.Acknowledgement;
import spark.network.protocol.Base;
import spark.utility.OutWrapper;

import java.util.Collection;
import java.util.HashMap;

public class GameEndedNotificationStrategy implements IProtocolHandleStrategy<GameEndedNotification> {
	private Logger logger = Logger.getLogger(GameEndedNotificationStrategy.class);

	private Game game;

	public GameEndedNotificationStrategy(Game game) {
		this.game = game;
	}

	@Override
	public void handle(GameEndedNotification packet, OutWrapper<Response> acknowledgement, Collection<Base> additionalData) {
		HashMap<Long, Result> winMap = new HashMap<Long, Result>();
		for(GameEndedNotification.Result result : packet.getResults()) {
			winMap.put(result.getFieldId(), result.getWon() ? Result.Win : Result.Lose);
		}
		game.setWinMap(winMap);
		game.end();
		acknowledgement.value = new Acknowledgement(packet);
	}
}